$(document).ready(function(){
	//窗口变化重定义尺寸事件
	$(window).resize(function(){
		setBackground();
		if($(window).width() < 1027){
			$("#background-img").width(1027);
			$("#surprised .slide-01 img").width(1027);
		}else{
			$("#background-img").width($(window).width());
			$("#surprised .slide-01 img").width($(window).width());	
		}
	});
	changeBackground();
	setBackground();
	setHeight();
	//微信图标hover展示
	$(".weixin-img").mouseenter(function(e){
		$(".code-for-weixin").show();
	});
	$(".weixin-img").mouseleave(function(e){
		$(".code-for-weixin").hide();
	});

	//鼠标移入移出导航事件
	$(".nav").delegate("a","mouseenter",function(e){
		$(e.target).prev().css("display","inline-block");
		$(e.target).next().css("display","inline-block");
	});
	$(".nav").delegate("a","mouseleave",function(e){
		$(e.target).prev().css("display","none");
		$(e.target).next().css("display","none");
	});

	//鼠标移入移出左侧菜单
	var canMoveMouseFlag = true;
	$(".for-left-nav").mouseenter(function(e){
		if(canMoveMouseFlag){
			canMoveMouseFlag = false;
			$(".nav-top").animate({left: '266px'}, "slow");
			$(".nav-bottom").animate({left: '266px'}, "slow",function(){
				canMoveMouseFlag = true;
			});
		}
		$(".menu a").css("background","url(res/menu2.png) no-repeat");
		$(".menu a").css("background-position","right center");
		$(".level-pager").css("display","none");
	});
	$(".left-nav").mouseleave(function(e){
		if(canMoveMouseFlag){
			canMoveMouseFlag = false;
			$(".nav-top").animate({left: '158px'}, "slow");
			$(".nav-bottom").animate({left: '-7px'}, "slow",function(){
				canMoveMouseFlag = true;
				$(".level-pager").css("display","block");
			});	
		}
		
		$(".menu a").css("background","url(res/menu.png) no-repeat");
		$(".menu a").css("background-position","right center");
	});

	//主要列表动画效果
	$(".float-div").delegate(".title-href","click",function(){
		var currentHeight = parseInt($(this).parent().css("top"));
		var totalLength = $(".float-div .about-list .about-item").length;
		//点击项已展开时
		if($(this).hasClass("opened")){
			$(this).parent().next().fadeOut("fast");
			var j = 0;
			for(var i = $(this).parent().parent().index();i < totalLength-1;i++){
				$(".about-item:eq("+(i+1)+") .li-title").animate({top: currentHeight+60+j*60+"px"}, "fast");
				j = j+1;
			}
			$(this).children(".show-info-btn").html("+");
			$(this).removeClass("opened");
		}else{
			//点击项未展开、且存在其他展开项时
			if($(".float-div").find(".opened").length >0){
				$(".float-div").find(".opened").parent().next().fadeOut("fast");
				var openedHeight =  parseInt($(".float-div").find(".opened").parent().css("top"));
				var contentHeight = parseInt($(this).parent().next().css("height"));
				var totalHeight = currentHeight + contentHeight + 7 + 60;
				var showEleHeight = parseInt($(this).parent().css("top"));
				var showEleTop = ($(this).parent().parent().index()+1)*60+50;
				var totalTop = ($(this).parent().parent().index()+1)*60 + 57 + parseInt($(this).parent().next().css("height"));
				var j = 0;
				var m = 0;
				for(var i = $(".float-div").find(".opened").parent().parent().index();i < $(this).parent().parent().index();i++){
					$(".about-item:eq("+(i+1)+") .li-title").animate({top: openedHeight+60+j*60+"px"}, "fast");
					j = j+1;
				}
				for(var i = $(this).parent().parent().index();i < totalLength-1;i++){
					$(".about-item:eq("+(i+1)+") .li-title").animate({top: totalTop+14+m*60+"px"}, "fast");
					m = m+1;
				}
				$(this).parent().next().fadeIn("fast");
				$(".float-div").find(".opened").children(".show-info-btn").html("+");
				$(".float-div").find(".opened").removeClass("opened");
				$(this).children(".show-info-btn").html("-");
				$(this).addClass("opened");
			}else{
				//点击项未展开、且不存在其他展开项时
				$(this).addClass("opened");
				var contentHeight = parseInt($(this).parent().next().css("height"));
				var totalHeight = currentHeight + contentHeight + 7 + 60;
				var j = 0;
				for(var i = $(this).parent().parent().index();i < totalLength-1;i++){
					$(".about-item:eq("+(i+1)+") .li-title").animate({top: totalHeight+7+j*60+"px"}, "fast");
					j = j+1;
				}
				$(this).children(".show-info-btn").html("-");
				$(this).parent().next().fadeIn("fast");
			}
		}
	});

	//工厂、办公环境图片动画
	$(".factory-header").click(function(){
		$(".factory-pic img").animate({left:"-1628px"},100000);
	});
	$(".office-header").click(function(){
		$(".office-pic img").animate({left:"-320px"},50000);
	});

	//员工照片
	$(".employee-show").click(function(){
		$(".float-div").fadeOut("fast",function(){
			$(".staf-max").css("z-index","1");
			$(".staf-max").fadeIn("slow");
		});
	});
	$(".staff-hide").click(function(){
		$(".staf-max").fadeOut("fast",function(){
			$(".staf-max").css("z-index","-40");
			$(".float-div").fadeIn("slow");
		});
	});
});
//初始化列表头高度
function setHeight(){
	$("#surprised .slide-01 img").css("height","949px");
	$(".background-img").css("height","949px");
	$(".background-img").css("background","url(res/background.png) repeat");
	$(".float-div").css("height",(document.documentElement.scrollHeight)+"px");
	
	var introHeight = parseInt($(".introduce-context").css("height")) + 112 + 7 + 10;
	$(".mission-header").css("top",introHeight+"px");
	$(".office-header").css("top",introHeight+60+"px");
	$(".factory-header").css("top",(introHeight+120)+"px");
	$(".award-header").css("top",(introHeight+180)+"px");
	$(".employee-header").css("top",(introHeight+240)+"px");
	$(".contact-header").css("top",(introHeight+300)+"px");
}

function changeBackground(){
	var picCount1 = 0;
	var picCount2 = 1;
	var i = 0;
	//标签计数
	var j = 1;
	var maxWidth = $(window).width();
	//alert(maxWidth);
	var timer = setInterval(changeBackImg,5);
	var listLeng = $("#surprised li").length;
	function changeBackImg(){
		//alert(i);
		i = i + 1;
		if(i>maxWidth){
			if( picCount1 == listLeng){
				picCount1 = 0;
				setVis($("#surprised li:eq("+picCount1+")").find("img"));
				$(".name a:eq("+picCount1+")").css("display","none");
				$(".name-info p:eq("+picCount1+")").css("display","none");
				picCount1 = picCount1 + 1;
				
			}else{
				setVis($("#surprised li:eq("+picCount1+")").find("img"));
				$(".name a:eq("+picCount1+")").css("display","none");
				$(".name-info p:eq("+picCount1+")").css("display","none");
				picCount1 = picCount1 + 1;
			}
			currentImg = picCount1;
			if(picCount2 == listLeng){
				picCount2 = 0;
				setOpa($("#surprised li:eq("+picCount2+")").find("img"));
				$(".name a:eq("+picCount2+")").css("display","block");
				$(".name-info p:eq("+picCount2+")").css("display","block");
				picCount2 = picCount2 + 1;
			}else{
				setOpa($("#surprised li:eq("+picCount2+")").find("img"));
				$(".name a:eq("+picCount2+")").css("display","block");
				$(".name-info p:eq("+picCount2+")").css("display","block");
				picCount2 = picCount2 + 1;
			}
			i = 0;
			if( j == listLeng){
				j = 0;
				var nameWidthValue = parseInt($(".name1").css("width")) + (document.documentElement.clientWidth*0.45) - 54;
				$(".project").css("left",(nameWidthValue)+"px");
				j = j + 1;
			}else{
				var nameWidthValue = parseInt($(".name a:eq("+j+")").css("width")) + (document.documentElement.clientWidth*0.45) - 54;
				$(".project").css("left",(nameWidthValue)+"px");
				j = j + 1;
			}
		}
		$("#progress-bar").css("left",i+"px");
	}
}
//隐藏前一个元素
function setVis(imgDom){
	var opaImg = 1;
		var timerO = setInterval(runO,10);
		function runO(){
			imgDom.css("z-index","-30");
			opaImg = opaImg - 0.05;
			imgDom.css("opacity",opaImg);
			if(imgDom.css("opacity") == 0){
				clearInterval(timerO);
				imgDom.css("display","none");
			}
		}
}
//展示下一个元素
function setOpa(imgDom){
	var opaImg = 0;
		var timerO = setInterval(runO,10);
		function runO(){
			imgDom.css("z-index","-10");
			imgDom.css("display","block");
			opaImg = opaImg + 0.05;
			imgDom.css("opacity",opaImg);
			if(imgDom.css("opacity") == 1){
				clearInterval(timerO);
			}
		}
}
function setBackground(){
	$("#surprised").width($(window).width());
	//$("#surprised").height($(window).height());
	$(".footer").height($(window).height()-381);
	if($(window).height() < 520){
		$(".footer").height(123);
	}
}